/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.JavaFX.TreeView;

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 *
 * @author kylin
 */
public class TestTreeView1 extends Application{
    private VBox box=null;
    
    public TreeView createTree(){
        Node rootIcon=new ImageView(new Image(getClass().getResourceAsStream("icon.png")));
        TreeItem<String> rootNode=new TreeItem<String>("Root",rootIcon);
        rootNode.setExpanded(true);
        TreeView<String> treeView=new TreeView<String>(rootNode);
        treeView.setEditable(true);
        treeView.setCellFactory(new Callback<TreeView<String>,TreeCell<String>>(){
            @Override
            public TreeCell<String> call(TreeView<String> param) {
                return new PeopleTreeCell();
            }
        });
        
        List<People> pl=new ArrayList<People>();
        for(int i=1;i<=10;i++){
            People p=null;
            if(i%2==0){
                p=new People("name"+i,"grade"+2);
            }else if(i%3==0){
                p=new People("name"+i,"grade"+3);
            }else{
                p=new People("name"+i,"grade"+5);
            }
            pl.add(p);
        }
        Image depIcon=new Image(getClass().getResourceAsStream("email.png"));
        for(People p:pl){
            //System.out.println("[name:"+p.getName()+"]"+" [grade:"+p.getGrade()+"]");
            TreeItem<String> leaf=new TreeItem<String>(p.getName());
            boolean found=false;
            for(TreeItem<String> depNode:rootNode.getChildren()){
                if(depNode.getValue().contentEquals(p.getGrade())){
                    depNode.getChildren().add(leaf);
                    found=true;
                    break;
                }
            }
            if(!found){
                TreeItem depNode=new TreeItem(p.getGrade(),new ImageView(depIcon));
                rootNode.getChildren().add(depNode);
                depNode.getChildren().add(leaf);
            }
        }
        return treeView;
    }
    
    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("Test TreeView1");
        box=new VBox();
        Scene scene=new Scene(box,400,300);
        scene.setFill(Color.LIGHTGRAY);
        box.getChildren().add(createTree());
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

class People{
    private SimpleStringProperty name;
    private SimpleStringProperty grade;
    
    public People(String name, String grade) {
        this.name=new SimpleStringProperty(name);
        this.grade= new SimpleStringProperty(grade);
    }

    public String getName() {
        return name.get();
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public String getGrade() {
        return grade.get();
    }

    public void setTel(String grade) {
        this.grade.set(grade);
    }
}
class PeopleTreeCell extends TreeCell<String>{
    private Label label=null;
    private ContextMenu addMenu=new ContextMenu();
    private Node iconfolder=null;
    private Node iconfile=null;
    
    public PeopleTreeCell(){
        MenuItem addMenuItem = new MenuItem("get info");
        addMenu.getItems().add(addMenuItem);
        addMenuItem.setOnAction(new EventHandler(){
            public void handle(Event t){
                System.out.println("[info]:"+PeopleTreeCell.this.getTreeItem().getValue());
            }
        });
        iconfolder=new ImageView(new Image(getClass().getResourceAsStream("icon.png")));
        iconfile=new ImageView(new Image(getClass().getResourceAsStream("camera.png")));
    }
    
//    @Override
//    public void startEdit(){
//        super.startEdit();
//        if(label==null){
//            createLabel();
//        }
//        setText(null);
//        setGraphic(label);
//    }
    
//    @Override
//    public void cancelEdit(){
//        super.cancelEdit();
//        setText((String)getItem());
//        setGraphic(getTreeItem().getGraphic());
//    }
    
//    @Override
//    public void updateItem(String item,boolean empty){
//        super.updateItem(item,empty);
//        if(empty){
//            this.setText(null);
//            this.setGraphic(null);
//        }else{
//            if(isEditing()){
//                if(label!=null){
//                    label.setText(getString());
//                }
//                setText(null);
//                setGraphic(label);
//            }else{
//                setText(getString());
//                setGraphic(getTreeItem().getGraphic());
//                if(!getTreeItem().isLeaf()&&this.getTreeItem().getParent()!=null){
//                    setContextMenu(addMenu);
//                }
//            }
//        }
//    }
    @Override
    public void updateItem(String item,boolean empty){
        super.updateItem(item, empty);
        if(empty){
            this.setText(null);
            this.setGraphic(null);
        }else{
            if(getTreeItem().isLeaf()){
                createLabel(iconfile,true);
                setContextMenu(addMenu);
            }else{
                createLabel(iconfolder,false);
            }
            setGraphic(label);
        }
    }
    private void createLabel(Node icon,boolean clickable){
        label=new Label(getString());
        label.setGraphic(icon);
        if(clickable){
            label.setOnMouseClicked(new EventHandler<MouseEvent>(){
                @Override
                public void handle(MouseEvent event) {
                    System.out.print("[itemtype]:"+getTreeItem().getValue());
                    System.out.print(" [value]:"+getString());
                    System.out.print(" [isleaf]:"+getTreeItem().isLeaf());
                    System.out.println(" [parent]:"+getTreeItem().getParent());
                }
            });
        }
    }
    private String getString(){
        return this.getItem()==null?"":getItem().toString();
    }
}